package com.cs.study.homework._0206;

import java.util.Arrays;

public class Delete {
    public static void main(String[] args) {
        String[] arr = new String[]{"abc", "123", "123", "123", "666", "777"};
        String element = "123";
        //过去删除元素个数
        int count = getLength(arr,element);
        //定义新数组长度
        String[] newArr = new String[arr.length - count];
        //得到新数组
        deleteElement(arr, newArr, element);
        System.out.println("原始数组：" + Arrays.toString(arr));
        System.out.println("新数组：" + Arrays.toString(newArr));

    }

    public static void deleteElement(String[] oddArray, String[] newArray, String element) {
        int count = 0;//新数组下标
        for (int i = 0; i < oddArray.length; i++) {
            if (!oddArray[i].equals(element)) {
                //元素依次放到新数组中
                newArray[count] = oddArray[i];
                count++;
            }
        }
    }


//获取新数组长度
    public static int getLength (String[] newArray, String element) {
        int count = 0;
        for (int i = 0; i < newArray.length; i++) {
            //计算被删除元素的个数
            if (newArray[i].equals(element)) {
                count++;
            }
        }
        return count;
    }

}